package cn.sciento.export.filler;

import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import cn.sciento.export.ExcelFiller;
import cn.sciento.export.vo.ExportColumn;
import org.springframework.util.Assert;

public class SqlSheetFiller extends ExcelFiller {
    public static final String FILLER_TYPE = "sql-sheet";

    public SqlSheetFiller() {
    }

    public SqlSheetFiller(SXSSFWorkbook workbook) {
        super(workbook);
    }

    public String getFillerType() {
        return "sql-sheet";
    }

    public void fillData(SXSSFWorkbook workbook, ExportColumn exportClass, List<?> data) {
        if (data == null)
            return;
        if (workbook.getNumberOfSheets() == 0)
            createSheetAndTitle(workbook, exportClass);
        for (int i = 0, len = data.size(); i < len; i++) {
            SXSSFSheet sheet = workbook.getSheetAt(i);
            sheet.setRandomAccessWindowSize(-1);
            fillData(sheet, exportClass.getChildren().get(i), (List) data.get(i));
        }
    }

    private void fillData(SXSSFSheet sheet, ExportColumn exportClass, List<?> data) {
        for (int i = 0, len = data.size(); i < len; i++) {
            Assert.isTrue((sheet.getLastRowNum() < 1000000), "export.too-many-data");
            SXSSFRow row = sheet.createRow(i + 1);
            fillRow(row, (Map<?, ?>) data.get(i), exportClass.getChildren());
        }
    }

    public void createSheetAndTitle(SXSSFWorkbook workbook, ExportColumn exportClass) {
        for (int i = 0, len = exportClass.getChildren().size(); i < len; i++) {
            SXSSFSheet sheet = workbook.createSheet("Sheet" + (i + 1));
            sheet.setDefaultColumnWidth(20);
            createTitleRow(sheet, exportClass.getChildren().get(i));
        }
    }

    private void createTitleRow(SXSSFSheet sheet, ExportColumn titleColumns) {
        throw new UnsupportedOperationException("in development...");
    }

    private void fillRow(SXSSFRow row, Map<?, ?> rowData, List<ExportColumn> columns) {
        throw new UnsupportedOperationException("in development...");
    }
}
